/* Copyright Paul Reiche, Fred Ford. 1992-2002, Christopher Night 2013

This file is part of the HTML5 port of Ur-Quan Masters (UQM-HTML5)

UQM-HTML5 is free software: you can redistribute it and/or modify it under the terms of the GNU
General Public License as published by the Free Software Foundation, either version 3 of the
License, or (at your option) any later version.

UQM-HTML5 is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
Public License for more details.

You should have received a copy of the GNU General Public License along with this program.  If not,
see <http://www.gnu.org/licenses/>. */

var NUM_SIDES = 2

// TODO: not sure where this lives
var battle_counter = [0, 0], battleFrameCount = 0


UFX.scenes.battle = {
	// Initially battle/Battle
	start: function () {
		race_q = [
			[
				new Ship(races.human),
			], [
				new Ship(races.human),
			]
		]

//		var num_ships = InitShips()
		misc.spawn_planet()
		for (var j = 0 ; j < 5 ; ++j) {
			misc.spawn_asteroid()
		}

		battle_counter[0] = race_q[0].length
		battle_counter[1] = race_q[1].length
		this.setupBattleInputOrder()
		battleFrameCount = 0  // only strictly needed for netplay
		
		// TODO: ship selection via pickmele/GetInitialMeleeStarShips
		for (var j = 0 ; j < NUM_SIDES ; ++j) {
			race_q[j].activeship = race_q[j][0]
			race_q[j].activeship.playerNr = j
			race_q[j].activeship.spawn()
		}
		
		context.hideAll()
		context.space.visible = true
		galaxy.init()
	},

	thinkargs: function () {
		return [UFX.key.state().pressed]
	},

	// initially battle/DoBattle
	think: function (kpressed) {
		++battleFrameCount
		if (!kpressed.space) return
		this.ProcessInput(kpressed)
		process.UpdateQueue()
	},
	draw: function () {
		process.RedrawQueue()
	},
	
	RunAwayAllowed: function () {
		return false
		// TODO: allow runaway during full game
	},
	
	setupBattleInputOrder: function () {
		// Will need to be different for net play (or, obv, NUM_SIDES > 2)
		this.battleInputOrder = [0, 1]
	},
	
	ProcessInput: function (kpressed) {
		for (var j = 0 ; j < NUM_SIDES ; ++j) {
			var side = this.battleInputOrder[j]
			var ship = race_q[side].activeship
			
			// TODO: move these hard-coded controls
			ship.input_state = {}
			if (side != 0) continue
			ship.input_state.LEFT = kpressed.left
			ship.input_state.THRUST = kpressed.up
			ship.input_state.RIGHT = kpressed.right
			ship.input_state.WEAPON = kpressed["1"]
			ship.input_state.SPECIAL = kpressed["2"]
			
			// TODO: invoke DoRunAway when appropriate
		}
	},
}

